<template>
  <view>
    <!-- 商品图片轮播图 -->
    <swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000">
      <swiper-item v-for="(item, i) in goodsDetail.pics" :key="i" @click="preview(i)">
        <image :src="item.pics_big_url" mode=""></image>
      </swiper-item>
    </swiper>
    <!-- 商品信息 -->
    <view class="goods-info" v-if="JSON.stringify(goodsDetail) !=='{}'">
      <view class="goods-top">
        ¥{{goodsDetail.goods_price}}
      </view>
      <view class="goods-middle">
        <view class="goods-middle-left">
          {{goodsDetail.goods_name}}
        </view>
        <view class="goods-middle-right">
          <uni-icons type="star"></uni-icons>
          <view class="">
            收藏
          </view>
        </view>
      </view>
      <view class="goods-bottom">
        快递：免运费
      </view>
    </view>

    <!-- 商品详细介绍 -->
    <rich-text :nodes="goodsDetail.goods_introduce"></rich-text>

    <!-- 商品导航 -->
    <view class="goods-nav" v-if="Object.keys(goodsDetail).length>0">
      <uni-goods-nav :fill="true" :options="options" :buttonGroup="buttonGroup" @click="onClick"
        @buttonClick="buttonClick" />
    </view>
  </view>
</template>

<script>
  import {
    mapMutations,
    mapState,
    mapGetters,
  } from 'vuex'

  export default {
    computed:{
      ...mapState('m_cart', ['cart']),
      ...mapGetters('m_cart', ['total']),
    },
    watch:{
      // total(newValue) {
      //   // 更新购物车数量
      //   this.options[1].info = newValue
      // }
      total: {
        handler(newValue) {
          // 更新购物车数量
          this.options[1].info = newValue
        },
        immediate: true
      }
    },
    data() {
      return {
        goodsDetail: {}, // 产品详情
        options: [{
          icon: 'shop',
          text: '店铺',
          infoBackgroundColor: '#007aff',
          infoColor: "red"
        }, {
          icon: 'cart',
          text: '购物车',
          info: 0
        }],
        buttonGroup: [{
            text: '加入购物车',
            backgroundColor: '#ff0000',
            color: '#fff'
          },
          {
            text: '立即购买',
            backgroundColor: '#ffa200',
            color: '#fff'
          }
        ]
      };
    },
    onLoad(options) {
      // 发起网络请求
      this.getGoodsDetail(options.goods_id)
    },
    methods: {
      ...mapMutations('m_cart', ['addToCart']),
      async getGoodsDetail(goods_id) {
        const {
          data: res
        } = await uni.$http.get('/goods/detail', {
          goods_id: goods_id
        })
        console.log(res)
        if (res.meta.status !== 200) return uni.$showMsg()

        // 赋值数据到data中
        res.message.goods_introduce = res.message.goods_introduce.replace(/<img /g,
          '<img style="vertical-align: middle;" ').replace(/webp/g, 'jpg')

        this.goodsDetail = res.message


      },
      // 轮播图预览
      preview(i) {
        console.log('===', i)
        uni.previewImage({
          urls: this.goodsDetail.pics.map(pic => pic.pics_big_url),
          current: i
        })
      },
      // 左边点击
      onClick(e) {
        console.log('左边 onClick', e)
        if (e.index === 1) {
          uni.switchTab({
            url: '/pages/cart/cart'
          })
        }
      },
      // 右边点击
      buttonClick(e) {
        console.log('右边 buttonClick', e)
        if (e.index === 0) {
          // 保存数据到 store/cart.js
          this.addToCart({
            goods_id: this.goodsDetail.goods_id,
            goods_name: this.goodsDetail.goods_name,
            goods_price: this.goodsDetail.goods_price,
            goods_count: 1,
            goods_small_logo: this.goodsDetail.goods_small_logo,
            goods_state: true
          })
          
          // this.options[1].info = this.total
        }
      }
    },

  }
</script>

<style lang="scss">
  swiper {
    height: 750rpx;

    swiper-item {
      image {
        width: 100%;
        height: 100%;
      }
    }
  }

  .goods-info {
    padding: 10px;
    padding-right: 0;

    .goods-top {
      color: #c00000;
      font-size: 18px;
      margin: 10px 0; // 上下10，左右0
    }

    .goods-middle {
      display: flex;
      justify-content: space-between;

      .goods-middle-left {
        font-size: 13px;
        padding-right: 10px;
      }

      .goods-middle-right {
        width: 120px;
        font-size: 12px;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        border-left: 1px solid #efefef;
        color: gray;
      }
    }

    .goods-bottom {
      margin: 10px 0;
      font-size: 12px;
      color: gray;
    }
  }

  .goods-nav {
    position: sticky;
    bottom: 0;
  }
</style>
